$m(function(args){
	var cache = {};
	var statusFormatter = function(val) {
		if(val == 'yes') {
			return TempoUtils.locale("mine.shenpitongguo");
		} else {
			return TempoUtils.locale("mine.weishenpi");
		}
	}
	
	var detailView = function(id) {
		var data = cache[id];
		dialog = $('.user-defined-edit-dailog').dialog({
			modal : true,
			autoOpen : false,
			width : 420,
			height : 540,
			title : data.name,
			resizable : false,
			close : function() {
				$('.user-defined-upload input').upload('destroy');
				$('.user-defined-upload').empty();
				$('.user-defined-edit-dailog').dialog('destroy');
			},
			open : function() {
				initForm(data, true);
			},
			buttons:[]
		}).dialog("open");
	}
	
	var initForm = function(data, full) {
		$('.user-defined-algorithm-info .text-value').empty();
		$('.user-defined-upload-form').remove();
		if(data) {
			var panel = $('.user-defined-algorithm-info');
			panel.find('.name').text(data.name || '');
			try{
				var v = TempoUtils.locale("mine." + data.subType);
				panel.find('.category').text(v);
			}catch(e){}
			panel.find('.lang').text(data.lang || '');
			panel.find('.username').text(data.userName || '');
			var d = DateUtils.formatDate(data.createDate);
			panel.find('.date').text(data.date || d || '');
			$('<textarea>').text(data.remark || '')
				.attr('readonly', true)
				.attr('title', data.remark || '')
				.appendTo(panel.find('.description'))
			var src = "";
			if(data.image) {
				src = contextPath + "/personal/image?src=" + data.image + "&baseResPath=" + data.baseResPath;
			} else if(data.icon) {
				src = "data:image/png;base64," + data.icon
			} else if(data.id) {
				src = contextPath + "/personal/image?id=" + data.id
			}
			$('<img>').attr('src', src).appendTo(panel.find('.image'));
			
		}
		$('.user-defined-algorithm-info textarea').css('height', '150px');
	}
	
	var operate = function(id) {
		var data = cache[id];
		var ele = $(this);
		var process = $("<div/>").tempoprogressbar();
		$.post(contextPath + '/personal/approve', {id: id}, function(d) {
			process.tempoprogressbar('destroy');
			if(d) {
				$('<div>').alert({
					msg: TempoUtils.locale("mine.caozuochenggong")
				});
				ele.parents('.user-defined-list-item')
					.find('.user-defined-alg-status')
					.text(TempoUtils.locale("mine.shenpizhuangtai") + statusFormatter("yes"));
				ele.remove();
			} else {
				$('<div>').alert({
					msg: TempoUtils.locale("mine.caozuoshibai")
				});
			}
		});
	}
	
	var createItem = function(data) {
		cache[data.id] = data;
		var icon = $('<div>').addClass('user-defined-alg-icon');
		$('<img>').attr('src', "data:image/png;base64," + data.icon).appendTo(icon);
		
		var name = $('<div>').addClass('user-defined-alg-name')
			.text(data.name).attr('title', data.name);
		var user = $('<div>').addClass('user-defined-alg-user')
			.text(TempoUtils.locale("mine.shenqingren") +data.userName).attr('title', data.userName);
		var status = $('<div>').addClass('user-defined-alg-status')
			.text(TempoUtils.locale("mine.shenpizhuangtai") + statusFormatter(data.approve)).attr('title', statusFormatter(data.approve));
		var lang = $('<div>').addClass('user-defined-alg-lang')
			.text(TempoUtils.locale("mine.leixing") +data.lang).attr('title', data.lang);
		var createDate = $('<div>').addClass('user-defined-alg-date')
			.text(TempoUtils.locale("mine.publish-all-flow-grid-column-createtime")+"：" + DateUtils.formatDateTime(data.createDate)).attr('title', DateUtils.formatDateTime(data.createDate));
		var action = $('<div>').addClass('user-defined-alg-action');
		$('<span>').addClass('user-defined-alg-action-view')
		.attr('uda-id', data.id)
		.button({
			label: TempoUtils.locale("mine.chakanxiangqin"),
			click: function(e) {
				var ui = $(this);
				var id = ui.attr('uda-id');
				detailView(id);
			}
		}).width('90px').appendTo(action);
		if(!data.approve) {
			$('<span>').addClass('user-defined-alg-action-publish')
			.attr('uda-id', data.id)
			.button({
				label: TempoUtils.locale("mine.tongyi"),
				click: function(e) {
					var ui = $(this);
					var id = ui.attr('uda-id');
					operate.call(this, id);
				}
			}).width('90px').appendTo(action);
		}
		
		var item = $('<div>').addClass('user-defined-list-item');
		item.append(icon);
		item.append(name);
		item.append(user);
		item.append(status);
		item.append(lang);
		item.append(createDate);
		item.append(action);
		return item;
	}
	
	
	var loadList = function() {
		$.post(contextPath + "/personal/listApproveAlgorithm", function(data) {
			if(data && data.rows && data.rows.length) {
				var list = $('.user-defined-mgmt-list').empty();
				for(d in data.rows) {
					var item = createItem(data.rows[d]);
					list.append(item);
				}
			}
		});
	}
	
	!function(){
		debugger
		loadList();
	}();
});